package com.carkeeper.user.controller;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.user.pojo.Employee;
import com.carkeeper.user.pojo.Role;
import com.carkeeper.user.service.AdminService;
import com.github.pagehelper.Page;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminController
 * @Description TODO
 * @Author yangbin
 * @Date 2020/11/25 14:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口api") //标签专门用来标注类的
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 为员工开通账号
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation("为员工开通账号，并授予权限")//标注方法
    public Result<Integer> updateAdmin(@RequestBody Employee employee, Integer roleId) {
        int i = adminService.updateAdmin(employee, roleId);
        if (i == 1) {
            return new Result<>(true, StatusCode.OK, "开户成功", i);
        }
        return new Result<>(false, StatusCode.ERROR, "开户失败");
    }

    /**
     * 查询用户列表
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有列表")//标注方法
    public Result<List<Map<String, Object>>> findAdminList() {
        List<Map<String, Object>> adminList = adminService.findAdminList();
        if (adminList != null) {
            return new Result<>(true, StatusCode.OK, "查询成功", adminList);
        }
        return new Result<>(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 修改用户状态
     * @param id
     * @return
     */
    @PutMapping("/lock/{id}")
    @ApiOperation("修改用户状态")//标注方法
    public Result updateStatus(@PathVariable("id") Integer id) {
        int i = adminService.updateStatus(id);
        if (i == 1) {
            return new Result(true, StatusCode.OK, "修改状态成功", i);
        }
        return new Result(false, StatusCode.ERROR, "修改状态失败");
    }

    /**
     * 用户列表分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    @ApiOperation("用户列表分页查询")//标注方法
    public Result findPage(@RequestParam(required = false) Map searchMap,
                           @PathVariable("currentPage") Integer currentPage,
                           @PathVariable("pageSize") Integer pageSize) {
        Page page = adminService.findPage(searchMap, currentPage, pageSize);
        if (page != null) {
            return new Result(true, StatusCode.OK, "删除成功", page);
        }
        return new Result(false, StatusCode.ERROR, "删除失败");
    }

    //根据用户名查询实体
    @GetMapping("/load/{username}")
    public Employee findUserInfo(@PathVariable("username") String username){
        return adminService.findById(username);
    }

    // @GetMapping
    // public Result<Boolean> login(@RequestParam("username") String username,
    //                              @RequestParam("password") String password){
    //     Boolean aBoolean = adminService.Login(username, password);
    //     return new Result<>(true,StatusCode.OK,"结果一致", aBoolean);
    // }
}
